package org.unt.url;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.http.ParseException;
import org.unt.logic.Strings;

public class Page {

	private String path;
	private boolean exist = false;
	private Set<Page> links = new HashSet<Page>();
	private String contents = null;

	private List<String> allChildren;
	private List<String> jipChildren;
	private List<String> noJipChildren;

	public static Site site = null;

	public Page(String parent, String relativePath) throws ParseException, IOException {

		if (parent != null) {
			relativePath = parent.substring(0, parent.lastIndexOf("/") + 1) + relativePath;
		}

		this.path = relativePath;
		System.out.println("Checking path " + relativePath);

		if (relativePath != null && !relativePath.toLowerCase().endsWith(".pdf")) {

			this.contents = site.fetchPageContents(relativePath);

			allChildren = Strings.getAllOcurrences(getContents(), "href=");
			jipChildren = new ArrayList<String>();
			noJipChildren = new ArrayList<String>();

			for (String child : allChildren) {

				if (child == null || "".equals(child.trim()) || child.startsWith("#")) {
					continue;
				}

				if (child.startsWith("http://")) {
					noJipChildren.add(child);
					continue;
				}

				jipChildren.add(child);
			}

			System.out.println("Found " + allChildren.size() + " children. JipChildren " + jipChildren.size() + ", noJipChildren " + noJipChildren.size());

		} else {
			System.out.println("Out of analysis");
		}
	}

	public List<String> getNoJipChildren() {
		return noJipChildren;
	}

	public boolean exist() {
		return contents != null && !(contents.indexOf("<H1>Not Found</H1>") > -1);
	}

	public List<String> getAllChildren() {
		return allChildren;
	}

	public List<String> getJipChildren() {
		return jipChildren;
	}

	// public List<String> agetAllChildrenPaths() throws ClientProtocolException, IOException {
	//
	// List<String> list = Strings.getAllOcurrences(getContents(), "href=");
	//
	// for (String childPath : list) {
	//
	// if (childPath == null || "".equals(childPath.trim()) || childPath.startsWith("#") || childPath.startsWith("http://"))
	// continue;
	//
	// String path = childPath.substring(0, childPath.lastIndexOf("/") + 1) + childPath;
	// Page childPage = site.fetchPage(path);
	//
	// if (childPage.getContents().indexOf("<H1>Not Found</H1>") > -1) {
	// System.out.println("PAGE NOT FOUND ! " + path);
	// } else {
	// System.out.println("OK " + childPath);
	// }
	// }
	//
	// return list;
	// }

	public boolean hasJipChildren() {
		return false;
	}

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	public void setExist(boolean exist) {
		this.exist = exist;
	}

	public Set<Page> getLinks() {
		return links;
	}

	public void setLinks(Set<Page> links) {
		this.links = links;
	}

	public String getContents() {
		return contents;
	}

	public void setContents(String contents) {
		this.contents = contents;
	}

}
